package com.cloudptt.wp.ui.more.advanced;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;

import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;
import com.cloudptt.wp.ui.BasePreferenceActivity;

public class UrgencyMsgSettingActivity extends BasePreferenceActivity
		implements OnPreferenceChangeListener, OnSharedPreferenceChangeListener {

	public static final String THIS_FILE = "UrgencyMsgSettingActivity";
	private EditTextPreference numPref = null;
	private EditTextPreference contentPref = null;
	
	private SharedPreferences sp = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		

		addPreferencesFromResource(R.xml.prefs_urgency_msg);
		//setContentView(R.layout.postioning_template);

		this.sp = PreferenceManager.getDefaultSharedPreferences(UrgencyMsgSettingActivity.this);
		this.sp.registerOnSharedPreferenceChangeListener(this);

		initView();
		
		initSummary();
		
		
	}
	
	private void initView(){
		
		numPref = (EditTextPreference)getPreferenceScreen().findPreference("pref_urgency_num"); 
		
		contentPref = (EditTextPreference)getPreferenceScreen().findPreference("pref_urgency_content"); 
	}
	
	private void initSummary()
	{
		Resources res = this.getResources();
		
		String key = res.getString(R.string.pref_urgency_num);
		String value = sp.getString(key, "");
		if(value != null)
		{
			if(value.length() > 0)
			{
				numPref.setSummary(value);
			}
			else
			{
				String str = res.getString(R.string.urgency_msg_num_summary);
				numPref.setSummary(str);
			}
		}
		
		key = res.getString(R.string.pref_urgency_content);
		
		value = sp.getString(key, "");
		if(value != null)
		{
			if(value.length() > 0)
			{
				contentPref.setSummary(value);
			}
			else
			{
				String str = res.getString(R.string.urgency_msg_contenct_summary);
				contentPref.setSummary(str);
			}
		}
		
	}

	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue) {
		Log.i(THIS_FILE, "onPreferenceChange:newValue="+newValue);
		return true;
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
		Log.i(THIS_FILE, "onSharedPreferenceChanged:key="+key );
		Resources res = this.getResources();
		String numKey = res.getString(R.string.pref_urgency_num);
		String contentKey = res.getString(R.string.pref_urgency_content);
		
		if(key.equals(numKey)
				|| key.equals(contentKey))
		{
			initSummary();
		}
	}

}
